package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jiyun.entity.UserRole;
import com.jiyun.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jiyun.entity.User;
import com.jiyun.service.IUserService;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;
    @Autowired
    IUserRoleService iUserRoleService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addUser(@RequestBody User user){
        userService.save(user);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateUser(@RequestBody User user){
        userService.updateById(user);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        userService.removeById(id);
        //删除中间表信息
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getUid,id);
        iUserRoleService.remove(userRoleLambdaQueryWrapper);
    }

    @RequestMapping("/userRoleList")
    public List<UserRole> userRoleList(@RequestParam Integer id){
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getUid,id);
        List<UserRole> list = iUserRoleService.list(userRoleLambdaQueryWrapper);
        return list;
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        userService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public User findById(@RequestParam Integer id){
        return userService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<User> findPage(@RequestParam Integer page,@RequestParam Integer pageSize,@RequestBody User user){
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.and(StringUtils.isNotBlank(user.getLoginName()),q->{
            q.like(User::getLoginName,user.getLoginName()).or().like(User::getUsername,user.getLoginName());
        });
        Page<User> userPage = new Page<>(page, pageSize);
        return userService.page(userPage,userLambdaQueryWrapper);
    }
    @RequestMapping("updateRole")
    public void updateRole(@RequestParam Integer uid,@RequestParam String rids){
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getUid,uid);
        iUserRoleService.remove(userRoleLambdaQueryWrapper);
        String[] rid = rids.split(",");
        for (String s : rid) {
            UserRole userRole = new UserRole();
            userRole.setUid(uid);
            userRole.setRid(Integer.parseInt(s));
            iUserRoleService.save(userRole);
        }
    }

}
